package com.alan.wifishare.utils;

import java.text.DecimalFormat;


/**
 * @ClassName:  FileSizeUtil   
 * @Description:  将文件大小转化成合适的单位
 * @author: Alan.wang  
 * @date:   2014-11-12 15:16  
 * version:1.0.0
 */ 

public class FileSizeUtil {
	
    /** gb to byte **/
    public static final long GB_2_BYTE = 1073741824;
    /** mb to byte **/
    public static final long MB_2_BYTE = 1048576;
    /** kb to byte **/
    public static final long KB_2_BYTE = 1024;

	public static String formatFileSize(long fileSize) {
        DecimalFormat df = new DecimalFormat("#.00"); 
        String fileSizeString = ""; 
        if (fileSize < KB_2_BYTE) { 
            fileSizeString = df.format((double) fileSize) + "B"; 
        } else if (fileSize < MB_2_BYTE) { 
            fileSizeString = df.format((double) fileSize / KB_2_BYTE) + "K"; 
        } else if (fileSize < GB_2_BYTE) { 
            fileSizeString = df.format((double) fileSize / MB_2_BYTE) + "M"; 
        } else { 
            fileSizeString = df.format((double) fileSize / GB_2_BYTE) + "G"; 
        } 
        return fileSizeString; 
    } 
}
